var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    encryptedData: '',
    iv: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  getPhoneNumber: function (e) {
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    this.setData({
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv
    });
    var self = this;
    getData(self);
    // getOpenId(self);
  }
})
//获取OpenId
var getOpenId = function (self) {
  wx.login({
    success: function (res) {
      console.log(res)
      wx.request({
        url: app.globalData.ServiceUrl + 'qiniufile/GetOpenid', //接口地址
        method: 'POST',
        data: {
          ValidateData: app.globalData.ValidateData,
          paras: {
            js_code: res.code
          }
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res.data)
          if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
            app.globalData.OPEN_ID = res.data.UserArea.openid;//获取到的openid
            app.globalData.SESSION_KEY = res.data.UserArea.session_key;//获取到session_key
            // wx.navigateBack({ changed: true });//返回上一页
            wx.setStorageSync('OPEN_ID', res.data.UserArea.openid)
            getData(self);
          } else {
            wx.showToast({
              title: '获取数据失败！',
              image: '../../images/img_jinggao.png',
              duration: 1000,
            })
          }
        }
      })
    }
  })
}
//获取数据
var getData = function (self) {
  wx.request({
    url: app.globalData.ServiceUrl + 'qiniufile/GetUserPhoneNumber', //接口地址
    method: 'POST',
    data: {
      ValidateData: app.globalData.ValidateData,
      paras: {
        encryptedData: self.data.encryptedData,
        iv: self.data.iv,
        session_key: app.globalData.SESSION_KEY,
      }
    },
    header: {
      'content-type': 'application/json' // 默认值
    },
    success: function (res) {
      console.log(res.data)
      if (res.data.BusinessInfo.Code == 1 && res.data.ReturnInfo.Code == 1) {
        wx.setStorageSync('phoneNumber', res.data.UserArea.phoneNumber)
        app.globalData.phoneNumber = res.data.UserArea.phoneNumber;
        wx.navigateBack({ changed: true });//返回上一页
      }
    }
  })

}